﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_HappyClubUpdForm
 * 类型：       控制层
 * 功能说明：   快乐管家-兴趣小组登记页面
 * 作者：       郭晓庆
 * 创建日期：   2007/09/13
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.System.Validator;
using Shpm.System.Web.Control;
using log4net;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using Shpm.App.Master.Dao;
using Shpm.App.Master.Bean;

public partial class App_Input_HappyClubUpdForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_HappyClubUpdForm));


    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // 保存接收的参数
            ViewState["InpInterestName"] = Convert.ToString(Context.Items["InpInterestName"]);
            ViewState["InpInterestType"] = Convert.ToString(Context.Items["InpInterestType"]);
            ViewState["InpLeadName"] = Convert.ToString(Context.Items["InpLeadName"]);


            ViewState["HidInterestName"] = Convert.ToString(Context.Items["HidInterestName"]);
            ViewState["HidInterestType"] = Convert.ToString(Context.Items["HidInterestType"]);
            ViewState["HidLeadName"] = Convert.ToString(Context.Items["HidLeadName"]);
            


            if (Context.Items.Contains("HidCommand"))
            {
                ViewState["HidCommand"] = Convert.ToString(Context.Items["HidCommand"]);
                ViewState["HidPageIndex"] = Context.Items["HidPageIndex"];
            }
            //俱乐部下拉列表邦定
            CommonDictionaryBean dataBean = new CommonDictionaryBean();
            CommonDictionaryDao Dao = new CommonDictionaryDao();
            dataBean.Catalog = "T_INTEREST_INTEREST_TYPE";
            this.dropInterestType.DataSource = Dao.FindByCommonDictionary(dataBean);
            this.dropInterestType.DataBind();

            //初始化画面
            if (Context.Items.Contains("INTEREST_ID"))
            {
                HappyClubDao happyClubDao = new HappyClubDao();
                HappyClubBean bean = new HappyClubBean();
                bean.InterestId = Convert.ToInt32(Context.Items["INTEREST_ID"]);
                bean = happyClubDao.GetInterest(bean);

                this.dropInterestType.SelectedValue = bean.InterestType;
                this.labInterestId.Text=bean.InterestId.ToString();
                this.txtInterestName.Text = bean.InterestName;

                this.txtLeadId.Text = bean.LeadId;
                this.txtLeadName.Text = bean.LeadName;

                this.txtAssistantId.Text = bean.AssistantId;
                this.txtAssistantName.Text = bean.AssistantName;

                this.txtEmployeeId.Text = bean.EmployeeId;
                this.txtEmployeeName.Text = bean.EmployeeName;
                this.txtBeginDate.Text = DateUtils.convertToString((DateTime)bean.BeginDate);

                
                this.txtMemo.Text = bean.Memo;


            }

            //删除按钮状态
            this.btnDel.Enabled = Context.Items.Contains("INTEREST_ID")?true:false;


            this.txtLeadId.Attributes.Add("onblur", "showMemberName(this.id,'" + this.txtLeadName.ClientID + "');");
            this.txtAssistantId.Attributes.Add("onblur", "showMemberName(this.id,'" + this.txtAssistantName.ClientID + "');");
            this.txtEmployeeId.Attributes.Add("onblur", "showEmployeeName(this.id,'" + this.txtEmployeeName.ClientID + "');");
        }
    }


    // 登记按钮
    protected void btnOk_Click(object sender, EventArgs e)
    {
        //check
        if( ComValidator1.DoValidator() == false)
        {
            ((Design_MasterPage)this.Master).InfoMessage = ComValidator1.ErrorMessage;
            return;
        }

        //会员id验证
        string msg = "";
        if (!(checkMemberNo(this.txtLeadId.Text.Trim(), ref msg)
            && checkMemberNo(this.txtAssistantId.Text.Trim(), ref msg)
            && checkEmployeeID(this.txtEmployeeId.Text.Trim(), ref msg)))
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0032").ToString().Replace("{0}", msg);
            return;
        }

        //取画面信息
        HappyClubDao dao = new HappyClubDao();
        HappyClubBean bean = new HappyClubBean();
        bean.InterestName = this.txtInterestName.Text;
        bean.InterestType = this.dropInterestType.SelectedValue;
        if (!string.IsNullOrEmpty(this.txtBeginDate.Text))
        {
            bean.BeginDate = DateUtils.convertToDate(this.txtBeginDate.Text);
        }

        bean.LeadId = "".Equals(this.txtLeadId.Text.Trim()) ? null : this.txtLeadId.Text.Trim();
        bean.AssistantId = "".Equals(this.txtAssistantId.Text.Trim()) ? null : this.txtAssistantId.Text.Trim();
        bean.EmployeeId = "".Equals(this.txtEmployeeId.Text.Trim()) ? null : this.txtEmployeeId.Text.Trim();
        bean.Memo = this.txtMemo.Text;

        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        bean.UserId = manager.getSysUserBean().getMstSysUserBean().SysUserId;

        //新建
        if (string.IsNullOrEmpty(this.labInterestId.Text))
        {
            int interestId=dao.Insert(bean);
           
            this.labInterestId.Text = interestId.ToString();
            ((Design_MasterPage)this.Master).InfoMessage =
                         HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();

            //删除按钮状态
            this.btnDel.Enabled = true;
        }
        //更新
        else 
        {
            bean.InterestId = Convert.ToInt32(this.labInterestId.Text);
            dao.Update(bean);
           
            ((Design_MasterPage)this.Master).InfoMessage =
                         HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
        }
        //删除按钮状态
        this.btnDel.Enabled = true;
    }

    private bool checkEmployeeID(string id,ref string msg)
    {
        msg = id;
        if(string.IsNullOrEmpty(id))
            return true ;

        EmployeeDao dao = new EmployeeDao();
        EmployeeBean bean = new EmployeeBean();
        bean.EmployeeId = id;
        if (dao.GetEmployee(bean) == null)
            return false;

        return true;

    }
    private bool checkMemberNo(string id,ref string msg)
    {
        msg = id;
        if(string.IsNullOrEmpty(id))
            return true ;

        MemberSrhDao dao = new MemberSrhDao();
        MemberSrhBean bean = new MemberSrhBean();
        bean = new MemberSrhBean();
        bean.MemberNo = id;
        if (dao.GetMember(bean) == null)
            return false;

        return true;
    }

    // 删除按钮
    protected void btnDel_Click(object sender, EventArgs e)
    {
        HappyClubDao dao = new HappyClubDao();
        HappyClubBean bean = new HappyClubBean();
        bean.InterestId = Convert.ToInt32(this.labInterestId.Text);
        dao.Delete(bean);
        ((Design_MasterPage)this.Master).InfoMessage =
                         HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();

        //画面更新
        this.labInterestId.Text = "";
        this.txtInterestName.Text = "";
        this.dropInterestType.SelectedIndex = 0;
        this.txtLeadId.Text = "";
        this.txtBeginDate.Text = "";
        this.txtMemo.Text = "";
        this.txtLeadName.Text = "";
        this.txtAssistantName.Text="";
        this.txtEmployeeName.Text = "";
        //删除按钮状态
        this.btnDel.Enabled = false;

    }

    // 返回按钮
    protected void btnBack_Click(object sender, EventArgs e)
    {

        // 传递控件输入内容



        Context.Items["InpInterestName"] =ViewState["InpInterestName"];
        Context.Items["InpInterestType"] =ViewState["InpInterestType"] ;
        Context.Items["InpLeadName"] = ViewState["InpLeadName"];

        // 传递接收的参数
        Context.Items["HidInterestName"] = Convert.ToString(ViewState["HidInterestName"]);
        Context.Items["HidInterestType"] = Convert.ToString(ViewState["HidInterestType"]);
        Context.Items["HidLeadName"] = Convert.ToString(ViewState["HidLeadName"]);
        if (ViewState.IsItemDirty("HidCommand"))
        {
            Context.Items["HidCommand"] = Convert.ToString(ViewState["HidCommand"]);
            Context.Items["HidPageIndex"] = Convert.ToString(ViewState["HidPageIndex"]);
        }
        string url = "~/App/Input/HappyClubSrhForm.aspx";
        ControllerUtils.Transfer(Context, url, true);    

    }
  

    
}
